1   /*
2    * Copyright (c) 2004-2005, University Health Network.  All rights reserved. Distributed under the BSD 
3    * license (see http://opensource.org/licenses/bsd-license.php).
4    *  
5    * Created on 5-Jan-2005
6    */
7   package ca.uhn.cache.internal.impl;
8   
9   import java.util.Date;
10  
11  import ca.uhn.cache.CacheReasonEnum;
12  import ca.uhn.cache.VolatilityEnum;
13  import ca.uhn.cache.impl.Chunk;
14  import ca.uhn.cache.impl.Query;
15  import ca.uhn.cache.internal.IChunk;
16  import junit.framework.TestCase;
17  
18  /***
19   * Unit tests for ChunkIterator.  
20   * 
21   * @author <a href="mailto:bryan.tripp@uhn.on.ca">Bryan Tripp</a>
22   * @version $Revision: 1.1 $ updated on $Date: 2005/01/24 22:52:21 $ by $Author: bryan_tripp $
23   */
24  public class ChunkIteratorTest extends TestCase {
25  
26      private IChunk myChunk1;
27      private IChunk myChunk2;
28      private IChunk myChunk3;
29      private IChunk myChunk4;
30      
31      /***
32       * Constructor for ChunkIteratorTest.
33       * @param theName ...
34       */
35      public ChunkIteratorTest(String theName) {
36          super(theName);
37      }
38          
39      protected void setUp() throws Exception {
40          super.setUp();
41          myChunk1 = makeChunk("1");
42          myChunk2 = makeChunk("2");
43          myChunk3 = makeChunk("3");
44          myChunk4 = makeChunk("4");
45      }
46      
47      private IChunk makeChunk(String theId) {
48          Date now = new Date();
49          return new Chunk(theId, VolatilityEnum.STABLE, now, now, now, new CacheReasonEnum[0], new Query()); 
50      }
51  
52      /***
53       */
54      public void testIteration() {
55          final ChunkIterator it = new ChunkIterator();
56          it.add(myChunk1);
57          it.add(myChunk2);
58          
59          assertTrue(it.hasNext());
60          assertEquals(myChunk1, it.next());
61          assertEquals(myChunk2, it.next());
62  
63          final IChunk chunk = myChunk3;
64          Runnable adder = new Runnable() {
65              public void run() {
66                  try {
67                      Thread.sleep(1000);
68                  } catch (InterruptedException e) {
69                  }
70                  it.add(chunk);
71              }
72          };        
73          Thread thread = new Thread(adder);
74          thread.start();
75          
76          assertTrue(it.hasNext());
77          assertEquals(myChunk3, it.next());
78          
79          it.add(myChunk4);
80          assertEquals(myChunk4, it.next());
81          
82          Runnable finisher = new Runnable() {
83              public void run() {
84                  try {
85                      Thread.sleep(1000);
86                  } catch (InterruptedException e) {
87                  }
88                  it.finished();
89              }
90          };
91          thread = new Thread(finisher);
92          thread.start();
93          
94          assertFalse(it.hasNext());
95      }
96      
97      
98      /***
99       */
100     public void testFinishBeforeIterate() {
101         final ChunkIterator it = new ChunkIterator();
102         it.add(myChunk1);
103         it.add(myChunk2);
104         it.finished();
105         
106         assertTrue(it.hasNext());
107         it.next();
108         it.next();
109         assertFalse(it.hasNext());
110     }
111 
112 }